import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hzy_project_template/models/image_normal_model.dart';
import 'package:hzy_project_template/utils/screenutil_tools.dart';
import 'package:image_picker/image_picker.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class GalleryTool {
  /// 图片压缩 并返回图片模型
  static configImageModelWithBytesCompress({
    int type = 0,
    Uint8List? imgthumbBytes,
    dynamic data,
    double? width,
  }) async {
    ImageNormalModel imageNormalModel = ImageNormalModel();
    imageNormalModel.byte = await configImageBytesCompress(
      type: type,
      imgthumbBytes: imgthumbBytes,
      data: data,
    );
    if (kIsWeb) {
      /// web 端待定
    } else {
      AssetEntity? aEntity = data;
      imageNormalModel.size = Size(
        width ?? SizeMacro.screenWidth,
        (width ?? SizeMacro.screenWidth) /
            ((aEntity?.size.width ?? 0) / (aEntity?.size.height ?? 1)),
      );
    }
    return imageNormalModel;
  }

  /// 图片压缩
  /// type 0 原图 1 1500 * 1500 2 500 * 500 3 150 * 150
  static Future<Uint8List?> configImageBytesCompress({
    int type = 0,
    Uint8List? imgthumbBytes,
    dynamic data,
  }) async {
    Uint8List? thumbBytes;
    if (imgthumbBytes == null) {
      if (kIsWeb) {
        XFile? xfile = data;
        Uint8List? originbty = await xfile?.readAsBytes();

        thumbBytes = originbty;
      } else {
        AssetEntity? aEntity = data;
        Uint8List? originbty = await aEntity?.originBytes;
        if (type == 0) {
          thumbBytes = originbty;
        } else if (type == 1) {
          thumbBytes = await aEntity?.thumbnailDataWithSize(
            const ThumbnailSize(1500, 1500),
          );
        } else if (type == 2) {
          thumbBytes = await aEntity?.thumbnailDataWithSize(
            const ThumbnailSize(500, 500),
          );
        } else if (type == 3) {
          thumbBytes = await aEntity?.thumbnailData;
        }
      }
    } else {
      thumbBytes = imgthumbBytes;
    }
    return thumbBytes;
  }
}
